package com.suishoupai.xmb.controller;

import com.github.pagehelper.Page;
import com.suishoupai.xmb.service.OrdersService;
import com.suishoupai.xmb.service.UserBuyService;
import com.suishoupai.xmb.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by caojian on 2017/4/20.
 */
@Controller
@RequestMapping("/userBuy")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class UserBuyController {
    @Autowired
    private UserBuyService userBuyService;
    @Autowired
    private OrdersService ordersService;

    @GetMapping("")
    public Object index(@RequestParam Map map, Model model) {
        PageUtil.startPage(map);
        Page page = (Page) userBuyService.getAllByScoreDesc();
        model.addAttribute("userBuys", page.toPageInfo());
        return "userBuy-list";
    }

    @GetMapping("/{userId}/orders")
    public Object orders(@PathVariable("userId") long userId, @RequestParam Map map, Model model) {
        PageUtil.startPage(map);
        Page page = (Page) ordersService.getOrdersListByUserId(userId);
        model.addAttribute("ordersList", page.toPageInfo());
        model.addAttribute("userId",userId);
        return "userBuy-orders-list";
    }
}
